﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using MyCityAzure.Models;

namespace MyCityAzure.Controllers
{
    public class CityController : ApiController
    {
        private MyCityEntities db = new MyCityEntities();

        // GET City --> returns a list of city names
        public IEnumerable<string> GetAllCityNames()
        {
            return db.Cities.OrderBy(c => c.Name).Select(c => c.Name);
        }

        // GET City/Dublin --> returns the city info
        [ResponseType(typeof(City))]
        public IHttpActionResult GetCityByName(string name)
        {
            City city = db.Cities.FirstOrDefault(c => c.Name.ToUpper() == name.ToUpper());
            if (city == null)
            {
                return NotFound();
            }

            return Ok(city);
        }

        // Get City/Population --> returns ordered city names by population
        [Route("city/population")]
        public IEnumerable<object> GetCityPopulation()
        {
            return db.Cities.Select(c => new { c.Name, c.Population }).OrderByDescending(c => c.Population);
        }

        // Get City/Area --> returns ordered city names by area
        [Route("city/area")]
        public IEnumerable<object> GetCityArea()
        {
            return db.Cities.Select(c => new { c.Name, c.Area }).OrderByDescending(c => c.Area);
        }

        // Get City/Density --> returns ordered city names by density
        [Route("city/density")]
        public IEnumerable<object> GetCityDensity()
        {
            return db.Cities.Select(c => new { c.Name, c.Density }).OrderByDescending(c => c.Density);
        }
    }
}